package joney.main.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class GenerateLargeXML {

	private static final String DATA = "Data";
	private static final String NAME = "name";
	private static final String ACCOUNT = "Account";
	private static final String EXPENSE = "Expense"; 
	private static final String TYPE = "type";
	private static final String AMOUNT = "amount";
	private static final String DATE = "date";
	private static final String OFFSET = "offset";
	
	
	public GenerateLargeXML()
	{
		go("data.xml");
		System.out.println("done");
	}
	
	
	private void go(String fileName)
	{
		Document doc= null;
		   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		   DocumentBuilder parser;
		try {
			parser = factory.newDocumentBuilder();
			doc = parser.newDocument();
		} catch (ParserConfigurationException e) {
			System.out.println(e);
		}
		
		Node root = doc.appendChild(doc.createElement(DATA));
		
		//add all the accounts
		for(int i=0; i < 50; i++)
		{
			Node accountNode = root.appendChild(doc.createElement(ACCOUNT));
			((Element)accountNode).setAttribute(NAME, "Account " + i); //write name
			
			
			for(int j=0; j < 2000; j++)
			{
				Node eNode = accountNode.appendChild(doc.createElement(EXPENSE));
				
				((Element)eNode).setAttribute(AMOUNT, String.valueOf(j));
				((Element)eNode).setAttribute(TYPE, "Type a");
				((Element)eNode).setAttribute(NAME, "e " + j);
				((Element)eNode).setAttribute(DATE, String.valueOf(new Date().getTime()));
			}
			
		}
		
		
		File xmlOutputFile = new File(fileName);
        FileOutputStream fos=null;
        Transformer transformer=null;
        try {
            fos = new FileOutputStream(xmlOutputFile, false);
        }
        catch (FileNotFoundException e) {
        	System.out.println(e);
        }
        // Use a Transformer for output
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        try {
            transformer = transformerFactory.newTransformer();
        }
        catch (TransformerConfigurationException e) {
        	System.out.println(e);
        }
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(fos);
        // transform source into result will do save
        try {
            transformer.transform(source, result);
        }
        catch (TransformerException e) {
        	System.out.println(e);
       }
	}
	
	
	public static void main(String[] args) {
		new GenerateLargeXML();

	}

}
